/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.data.controller.DistributionSemenCtlr;
import org.gov.vis.data.controller.DistributionSemenCtlrImpl;
import org.gov.vis.data.dto.DistributionSemenDto;
import org.gov.vis.data.gui.EditDistributionSemenDialog;
import org.gov.vis.data.gui.ViewDistributionSemenDialog;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class EditDistributionSemenListener implements ActionListener,BaseListener{




	final private ResourceBundle rb = Util.getResources();

	private DistributionSemenCtlr distributionSemenCtlr = new DistributionSemenCtlrImpl();

	private EditDistributionSemenDialog editDistributionSemenDialog;

	private ErrorsDisplayJPanel errorMsgPanel;
	private DistributionSemenDto distributionSemenDto;

	public EditDistributionSemenListener(EditDistributionSemenDialog editDistributionSemenDialog) {
		// TODO Auto-generated constructor stub
		this.editDistributionSemenDialog = editDistributionSemenDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		final EditDistributionSemenDialog _editDistributionSemenDialog = (EditDistributionSemenDialog)_guiObject;
		distributionSemenDto = new DistributionSemenDto();
		distributionSemenDto.setBreed(_editDistributionSemenDialog.getTxtFldDSBreed().getText());
		distributionSemenDto.setBullNo(_editDistributionSemenDialog.getTxtFldDSBullNo().getText());
		distributionSemenDto.setCode(_editDistributionSemenDialog.getTxtFldDSCode().getText());
		distributionSemenDto.setDiscord(_editDistributionSemenDialog.getTxtFldDSDiscord().getText());
		//distributionSemenDto.setDoDist(DU.parse(_distributionSemenDialog.getTxtFldDSDODistribution().getText()));
		if(_editDistributionSemenDialog.getDtChoDateOfDistribution().getDate() != null){
			distributionSemenDto.setDoDstrbut(_editDistributionSemenDialog.getDtChoDateOfDistribution().getDate());
		}

		distributionSemenDto.setNoStrawas(_editDistributionSemenDialog.getTxtFldDSNoOfStraws().getText());
		distributionSemenDto.setValue(_editDistributionSemenDialog.getTxtFldDSValue().getText());
		distributionSemenDto.setDisSemenId((Long)_editDistributionSemenDialog.getHidnObject());


	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub

		errorMsgPanel = new ErrorsDisplayJPanel(editDistributionSemenDialog.getjPanel1());
		errorMsgPanel.clearMsgPanel();


		if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){

			//Method called to either Add or Edit the GopalaMithra details
			addOrEditDistributionSemen();
		}

		if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
			
			int response = JOptionPane.showConfirmDialog(null, GuiConstants.DELETE_CONFIRM_MSG, "Confirm",
			        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			    
		    if (response == JOptionPane.YES_OPTION) {
		      // User clicked YES.
		    	deleteDistributionSemen(actionEvent);
		    	String searchText = editDistributionSemenDialog.getSearchText();

				// It is basically doing back button functionality. Please, understand the logic and write the comments.
				//Replace double quotes with appropriate Dialog title.
				ViewDistributionSemenDialog viewDistSemenDialog = new ViewDistributionSemenDialog(Main.getMainFrame(), "View Distribution Semen", false);
				for(ActionListener actionListener: viewDistSemenDialog.getSearchBtn().getActionListeners()) { 
					ViewDistributionSemenListener viewDistSemenList = (ViewDistributionSemenListener)actionListener;
					viewDistSemenDialog.getCodeTxtBx().setText(searchText);

					viewDistSemenList.actionPerformed(new ActionEvent(viewDistSemenDialog.getSearchBtn(), 1, rb.getString("dialog_button_serach")));
				}

		    	
		    } else if (response == JOptionPane.NO_OPTION || response == JOptionPane.CLOSED_OPTION) {
		      return;
		    }

		}
	}

	private void addOrEditDistributionSemen() {

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(editDistributionSemenDialog);

		try{
			//Method called to Add or Edit the GopalaMithra
			distributionSemenCtlr.addOrEditDisSemen(distributionSemenDto);
			JOptionPane.showMessageDialog(editDistributionSemenDialog, rb.getString(GuiConstants.DATA_EDIT_DISTRBUTIONSEMEN_SUCCESS));
			editDistributionSemenDialog.dispose();
			
			String searchText = editDistributionSemenDialog.getSearchText();

			// It is basically doing back button functionality. Please, understand the logic and write the comments.
			//Replace double quotes with appropriate Dialog title.
			ViewDistributionSemenDialog viewDistSemenDialog = new ViewDistributionSemenDialog(Main.getMainFrame(), "View Distribution Semen", false);
			for(ActionListener actionListener: viewDistSemenDialog.getSearchBtn().getActionListeners()) { 
				ViewDistributionSemenListener viewDistSemenList = (ViewDistributionSemenListener)actionListener;
				viewDistSemenDialog.getCodeTxtBx().setText(searchText);

				viewDistSemenList.actionPerformed(new ActionEvent(viewDistSemenDialog.getSearchBtn(), 1, rb.getString("dialog_button_serach")));
			}

		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = DistributionSemenCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_dist_semen_code").toUpperCase())){
						editDistributionSemenDialog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_semen_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_dist_semen_nostrwas").toUpperCase())){
						editDistributionSemenDialog.getLblNoOfStraws().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_semen_nostrwas").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_dist_semen_dtofdistr").toUpperCase())){
						editDistributionSemenDialog.getLblDODistribution().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_semen_dtofdistr").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_dist_semen_bullno").toUpperCase())){
						editDistributionSemenDialog.getLblBullNo().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_dist_semen_bullno").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					errorMsgPanel.setErrorMessages(errorMsgLst);					
				} else {
					//log.debug(e.getMessage(), e);
				}
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);
			}
		}		


	}

	private void deleteDistributionSemen(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		Long distributionSemensId = (Long)editDistributionSemenDialog.getHidnObject();
		try {
			distributionSemenCtlr.deleteDisSemen(distributionSemensId);
			JOptionPane.showMessageDialog(editDistributionSemenDialog, rb.getString(GuiConstants.DATA_DELETE_DISTRBUTIONSEMEN_SUCCESS));
			editDistributionSemenDialog.dispose();
		} catch (Exception e) {
			List<String> errorMsgLst = new ArrayList<String>();
			errorMsgLst.add(e.getMessage());
			errorMsgPanel.setErrorMessages(errorMsgLst);

		}
	}

}//end class
